<template>
  <div class="upload-img">
    <el-upload action="#" :show-file-list="false" :http-request="httpRequest">
      <div class="main">
        <img v-if="value" v-errorimg :src="value" alt="">
        <i v-else class="el-icon-plus" />
        <!-- 删除 -->
      </div>
    </el-upload>
    <!-- 删除 -->
    <i v-if="value" class="del el-icon-delete-solid" @click="del" />
    <!-- 预览按钮 -->
    <i v-if="value" class="big el-icon-zoom-in" @click="isShow = true" />
    <!-- 大图片对话框 -->
    <el-dialog title="预览" width="600px" :visible.sync="isShow">
      <div style="text-align: center">
        <img style="width: 100%" :src="value" alt="">
      </div>
    </el-dialog>
    <!-- 进度条 -->
    <el-progress
      v-if="showProgress"
      class="progress"
      type="circle"
      :width="180"
      :percentage="percentage"
    />
  </div>
</template>
<script>
import COS from 'cos-js-sdk-v5'
export default {
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      showProgress: false,
      isShow: false,
      percentage: 0,
      imgSrc: '',
      cos: ''
    }
  },
  created() {
    // 验证身份
    this.cos = new COS({
      SecretId: 'AKIDf7JpO2CbXQO7dqulNfKnF0CfuxznsAqF',
      SecretKey: 'TH1c6g3qNjriqdJkhTBfkZ8SVTB5oHZm'
    })
  },
  methods: {
    httpRequest(res) {
      console.log(res.file)
      this.showProgress = true
      this.cos.uploadFile(
        {
          Bucket: 'dhf-80-1305249343' /* 填写自己的bucket，必须字段 */,
          Region: 'ap-guangzhou' /* 存储桶所在地域，必须字段 */,
          Key:
            'hr91/' +
            res.file
              .name /* 存储在桶里的对象键（例如:1.jpg，a/b/test.txt，图片.jpg）支持中文，必须字段 */,
          Body: res.file, // 上传文件对象
          onProgress: (progressData) => {
            // 上传进度
            this.percentage = +(progressData.percent * 100).toFixed(2)
            if (progressData.percent === 1) {
              this.showProgress = false
            }
            console.log(JSON.stringify(progressData))
          }
        },
        (err, data) => {
          if (err) {
            console.log('上传失败', err)
          } else {
            console.log('上传成功', data)
            this.$emit('input', 'http://' + data.Location)
            // this.imgSrc = 'http://' + data.Location
          }
        }
      )
    },
    del() {
      this.$emit('input', '')
    }
  }
}
</script>
<style lang="scss" scoped>
.main {
  width: 180px;
  height: 180px;
  border: 1px dashed #ccc;
  display: flex;
  justify-content: center;
  align-items: center;
  img {
    width: 100%;
    height: 100%;
  }
  i {
    font-size: 30px;
  }
}
.upload-img {
  position: relative;
  width: 180px;
  &:hover {
    .del,
    .big {
      display: block;
    }
  }
  .del,
  .big {
    position: absolute;
    right: 10px;
    top: 5px;
    font-size: 20px;
    display: none;
    cursor: pointer;
  }
  .big {
    right: 40px;
  }
  .progress {
    position: absolute;
    left: 0;
    top: 0;
    background-color: rgba(0, 0, 0, 0.5);
    color: #f40;
  }
}
</style>
